var nameok="no",phoneok="no",bankok="no",testok="no",gettestbtn="noclicked";
var username,phonenumber,testnum;
var usermoney;
var id= $.cookie("MYID");
//初始获得焦点
document.getElementById('username').focus();

//所有输入框的判断
function inputtest(){
    $("#noteword").attr("class","");
    var usernameval=$("#username").val();//姓名
    var banknumberval=$("#banknumber").val();//银行卡号
    var phonenumberval=$("#phonenumber").val();//手机号
    //var testnumberval=$("#testnumber").val();//验证码
    if(!usernameval||/^\s+$/.test(usernameval)){
        $("#noteword").attr("class","wrong");
        $("#noteword").html("请输入持卡人姓名");
        $("#testtime").attr("class","rt");
    }
    //------------------姓名通过
    else{
        //已经填写银行卡
        if(banknumberval){
            //---------------银行卡通过
            if(luhmCheck(banknumberval)){
                //提现展示银行卡号
                $("#showbanknumber").html(banknumberval);
                //结果展示银行卡
                $("#banknum").html(banknumberval);
                //已经填写手机号
                if(phonenumberval){
                    if(/^1[34578]\d{9}$/.test(phonenumberval)){ //---------------手机号通过
                        $("#testtime").attr("class","rt canclick");  //--验证码按钮可以点击
                        return true;
                    }else{
                        $("#noteword").attr("class","wrong");
                        $("#noteword").html("手机号有误");
                        $("#testtime").attr("class","rt");
                    }
                }
                //没有填写手机号
                else{
                    $("#noteword").attr("class","wrong");
                    $("#noteword").html("请输入手机号");
                    $("#testtime").attr("class","rt");
                }
            }else{
                $("#noteword").attr("class","wrong");
                $("#noteword").html("银行卡有误");
                $("#testtime").attr("class","rt");
            }
        }
        //没有填写银行卡
        else{
            $("#noteword").attr("class","wrong");
            $("#noteword").html("请输入银行卡号");
            $("#testtime").attr("class","rt");
        }
    }
}

//获取用户余额
$.ajax({
    type:"get",
    datatype:"json",
    url:lastscoreurl+id,
    success:function(d){
        console.log(d);
        usermoney= Math.round(d.Data)/100;
        $("#lastmoney").html(usermoney+"元");
    }
});

//监听姓名输入
$("#username").on("input propertychange",function(e){
    $("#username").val($("#username").val().replace(/\,|\?|\#|\@|\%|\&|\*|\!|\~|\/|\t|\v|\n|\f/ig,""));//
    $("#noteword").html("请输入持卡人的银行卡及手机号");
    $("#noteword").attr("class","");
    //填信息"下一步"按钮样式
    var usernameval=$("#username").val().replace(/\s/g,"").replace(/[0-9]/g,"");//姓名
    $("#username").val(usernameval);
    var banknumberval=$("#banknumber").val();//银行卡号
    var phonenumberval=$("#phonenumber").val();//手机号
    if(usernameval&&banknumberval&&phonenumberval){
        $("#testtime").attr("class","rt canclick");
    }else{
        $("#testtime").attr("class","rt");
    }
});

//监听手机号输入是否正确
$("#phonenumber").on("input propertychange",function(e){
    //inputtest();
    $("#noteword").html("请输入持卡人的银行卡及手机号");
    $("#noteword").attr("class","");
    if(/^1[34578]\d{9}$/.test(e.target.value)){
        //phoneok="yes";
        phonenumber=e.target.value;
        //填信息"下一步"按钮样式
        var usernameval=$("#username").val();//姓名
        var banknumberval=$("#banknumber").val();//银行卡号
        var phonenumberval=$("#phonenumber").val();//手机号

        if(usernameval&&banknumberval&&phonenumberval){
            $("#testtime").attr("class","rt canclick");
        }else{
            $("#testtime").attr("class","rt");
        }
    }else{
        $("#testtime").attr("class","rt");
        //phoneok="no";
        //$("#usernextbtn").attr("class","");
        $("#noteword").html("手机号有误！");
        $("#noteword").attr("class","wrong");
    }
});

//监听银行卡输入是否正确
$("#banknumber").on("input propertychange",function(e){
    //inputtest();
    $("#noteword").html("请输入持卡人的银行卡及手机号");
    $("#noteword").attr("class","");
    if(luhmCheck(e.target.value)){
        //bankok="yes";
        //提现展示银行卡号
        $("#showbanknumber").html(e.target.value);
        //结果展示银行卡
        $("#banknum").html(e.target.value);
        //填信息下一步按钮样式
        var usernameval=$("#username").val();//姓名
        var banknumberval=$("#banknumber").val();//银行卡号
        var phonenumberval=$("#phonenumber").val();//手机号
        if(usernameval&&banknumberval&&phonenumberval){
            $("#testtime").attr("class","rt canclick");
        }else{
            $("#testtime").attr("class","rt");
        }
    }else{
        $("#testtime").attr("class","rt");
        //bankok="no";
        //$("#usernextbtn").attr("class","");
        $("#noteword").html("银行卡有误！");
        $("#noteword").attr("class","wrong");
    }
});

//监听有没有输入验证码
$("#testnumber").on("input propertychange",function(e){
    inputtest();
});

//获取验证码操作
$("#testtime").click(function(e){
    if(/canclick/.test(e.target.className)){
        if(inputtest()===true){
            if(gettestbtn==="noclicked"){
                gettestbtn="clicking";
                //给手机发送验证码
                var clocktim=60;
                $("#testtime").html(clocktim);
                $.ajax({
                    type:"get",
                    dataType: "json",
                    url:"https://www.pulu.com/webapi/api/Sms/GetValidCode?phoneNum="+phonenumber+"&isdesposit=1",
                    success:function(data){
                        var clocktim=60;
                        console.log(data);
                        var secondtime=setInterval(function(){
                            if(clocktim>0){
                                clocktim--;
                                $("#testtime").html(clocktim);
                            }else{
                                clearInterval(secondtime);
                                $("#testtime").html("获取验证码");
                                clocktim=60;
                                gettestbtn="noclicked";
                            }
                        },1000);
                    }
                });
            }
        }
    }
});

//用户信息点击“下一步”按钮 验证验证码
$("#usernextbtn").click(function(e){
    var testnum=$("#testnumber").val();
    //if(/canclick/.test(e.target.className)){
    if(inputtest()===true){
        $.ajax({
            type:"get",
            datatype:"json",
            url:testphoneurl+phonenumber+"&code="+testnum,
            success:function(d){
                console.log(d);
                if(d.Message==="验证码正确"){
                    $("#money").css("display","block");
                    $("#user").css("display","none");
                    //
                    $("#moneyheader").css("display","block");
                }else{
                    $("#noteword").attr("class","wrong");
                    $("#noteword").html("验证码有误");
                }
            }
        });
        //
        //$("#money").css("display","block");
        //$("#user").css("display","none");
        ////
        //$("#moneyheader").css("display","block");
    }
});

//监听用户输入金额
$("#withdrawalinput").on("input propertychange",function(e){
    console.log(usermoney);
    //console.log(e.target.value);
    console.log(e.target.value<=usermoney);
    if(e.target.value&&e.target.value<=usermoney){
        if(/[^0-9]/.test(e.target.value)){
            $("#withdrawalinput").val($("#withdrawalinput").val().replace(/[^0-9]/,""));
        }
        if(/^0*/.test(e.target.value)){
            $("#withdrawalinput").val($("#withdrawalinput").val().replace(/^0*/,""));
        }
        //if(/./.test(e.target.value)){
        //    $("#withdrawalinput").val($("#withdrawalinput").val().replace(/./,""));
        //}
        //if(/^[1-9]$|^[1-9][0-9]{1,3}$|^[1-9][0-9]{1,3}\.\d{1,2}$|^[1-9]{1,4}\.\d{1,2}$|^0\.[1-9]$|^0\.[0-9][1-9]$/.test(e.target.value)){
        if(/^[1-9]+$/.test(e.target.value)){
            $("#withdrawalbtn").attr("class","canclick");
            //提现金额
            $(".allmoney").html(e.target.value+"元");
            $("#showmoney").html(e.target.value+"元");
            //手续费
            $(".taxmoney").html(Math.round(e.target.value)/100+"元");
            $("#taxmoney").html(Math.round(e.target.value)/100+"元");
        }else{
            console.log("error");
            $("#withdrawalbtn").attr("class","");
        }
    }else{
        $("#withdrawalbtn").attr("class","");
    }
});

//提现按钮
$("#withdrawalbtn").click(function(e){
    if(/canclick/.test(e.target.className)){
        //if(parseFloat($("#withdrawalinput").val())<50){
        //    alert("提现金额不能少于50元");
        //}else{
        //    $("#taxnote").css("display","block");
        //}
        $("#taxnote").css("display","block");
    }
});

//取消提现
$("#taxnobtn").click(function(){
    $("#taxnote").css("display","none");
});

//确定提现
$("#taxokbtn").click(function(){
    //向服务器发请求 成功展示页面
    $("#waitplace").css("display","block");
    $.ajax({
        type:"post",
        datatype:"json",
        url:"https://www.pulu.com/webapi/api/Desposit/Adddesposit",
        data:{"userid":id,"cardNum":$("#banknumber").val(),"phoneNum":$("#phonenumber").val(),"realName":$("#username").val(),"amount":parseFloat($("#withdrawalinput").val())},
        success:function(d){
            $("#waitplace").css("display","none");
            $("#examine").css("display","block");
            $("#taxnote").css("display","none");
            $("#money").css("display","none");
            $("#moneyheader").css("display","none");
        }
    });
});

//头部返回按钮
$("#mainheader div").click(function(){
    history.go(-1);
});

//填写金额返回
$("#moneyheader div").click(function(){
    $("#money").css("display","none");
    $("#moneyheader").css("display","none");
    $("#user").css("display","block");
});

//申请已提交返回
$("#examineheader>div").click(function(){
    $("#examine").css("display","none");
    $("#money").css("display","block");
    $("#moneyheader").css("display","block");
});

//申请已提交确定
$("#finishbtn").click(function(){
    history.go(-1);
});